<template>
  <el-card>
    <div slot="header" style="line-height: 40px">
      <h1>添加管理员</h1>
    </div>
    <el-form
      ref="adminsForm"
      :rules="rules"
      label-width="120px"
      :model="adminsForm"
    >
      <el-form-item label="序号" v-show="false">
        <el-input v-model="adminsForm.id" disabled></el-input>
      </el-form-item>
      <el-form-item label="管理员名" prop="aname">
        <el-input v-model.trim="adminsForm.aname"></el-input>
      </el-form-item>
      <el-form-item label="管理员账号" prop="aaccount">
        <el-input v-model.trim="adminsForm.aaccount"></el-input>
      </el-form-item>
      <!-- <el-form-item label="密码" prop="apassword">
        <el-input
          type="password"
          v-model.trim="adminsForm.apassword"
        ></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="角色权限" prop="role">
        <el-input v-model.trim="adminsForm.role"></el-input>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import adminsApi from '@/http/adminsApi';

export default {
  data() {
    return {
      adminsForm: {
        aname:'',
        aaccount:'',
      },
      rules: {
        aname: [
          { required: true, message: '请输入管理员名', trigger: 'blur' },
          {
            min: 3,
            max: 15,
            message: '长度在 3 到 15 个字符',
            trigger: 'blur',
          },
        ],
        aaccount: [
          { required: true, message: '请输入管理员账号', trigger: 'blur' },
          {
            min: 3,
            max: 15,
            message: '长度在 3 到 15 个字符',
            trigger: 'blur',
          },
        ],
        // apassword: [
        //   { required: true, message: '请输入密码', trigger: 'blur' },
        //   {
        //     min: 3,
        //     max: 15,
        //     message: '长度在 3 到 15 个字符',
        //     trigger: 'blur',
        //   },
        // ],
      },
    };
  },
  created() {
    this.getEditObj();
  },
  methods: {
    onSubmit() {
      this.$refs['adminsForm'].validate(valid => {
        if (valid) {
          console.log('submit!', this.adminsForm);
          adminsApi.save(this.adminsForm).then(res => {
            console.log(res);
            if (res.data.code == 200) {
              this.$router.replace('/account/admins.html');
            } else {
              this.$message({
                showClose: true,
                message: res.data.msg,
                type: 'error',
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false; //阻止表单提交
        }
      });
    },
    onReset() {
      this.$refs['adminsForm'].resetFields();
    },
    getEditObj() {
      let id = this.$route.params.id;
      if (id) {
        adminsApi.edit(id).then(res => {
          if (res.data.code == 200) {
            this.adminsForm = res.data.data;
          }
        });
      }
    },
  },
};
</script>

<style></style>
